package cn.lucky.springcloud.controller;

import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lingpeng.jiang
 * @create 2021-01-30-14:14
 */
@RestController
@Slf4j
public class FlowLimitController {

    private int count;
    @GetMapping("/testA")
    public String testA(){
        return "----------testA";
    }


    @GetMapping("/testB")
    public String testB() throws BlockException {
        log.info("--------- currentThread:" + Thread.currentThread().getName() + "------ 次数: " + (++count));
        //ThreadUtil.sleep(800);
        return "----------testB";
    }

    @GetMapping("/testD")
    public String testD(){
        ThreadUtil.sleep(500);
        log.info("RT " + Thread.currentThread().getName());
        return "----------testD";
    }


    @GetMapping("/testE")
    public String testE(){
        //ThreadUtil.sleep(500);
        log.info("RT " + Thread.currentThread().getName());
        int i = 10 / 0;
        return "----------testD";
    }

    @GetMapping("/hotkey")
    @SentinelResource(value = "hotkey",blockHandler = "deal_hotkey")
    public String hotKey(@RequestParam(value = "p1",required = false) String p1,@RequestParam(value = "p2",required = false) String p2){
        return "---------- hotKey";
    }

    public String deal_hotkey(String p1,String p2,BlockException e){
        return "---------hotkey 降级了";
    }


    @GetMapping("/fallback")
    @SentinelResource(value = "fallback",blockHandler = "fallbackHandle")
    public String fallback(){
        log.info("-------------- 还没到降级的时候 ----------");
        ThreadUtil.sleep(1000);
        return "----- 真实方法";
    }

    public String fallbackHandle(){
        log.info("-------------- 降级了--------------");
        return "---- 降级方法";
    }

}
